﻿
@{
    ViewData["Title"] = "ManageChangepd";
    Layout = "~/Views/Shared/_SmallWinLayout.cshtml";
}

<div class="page-container">
    <div class="form form-horizontal">
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>原密码：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input name="oldpassword" id="oldpassword" class="input-text auth" type="password" placeholder="密码">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>新密码：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input name="newpassword" id="newpassword" class="input-text auth" type="password" placeholder="设置密码">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>再次输入新密码：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input name="newpassword2" id="newpassword2" class="input-text auth" type="password" placeholder="确认新密码">
            </div>
        </div>
        <div class="row cl">
            <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                <input class="btn btn-primary radius" onclick="savePassd('/Home/ManageSavePassword')" type="submit" value="&nbsp;&nbsp;提交&nbsp;&nbsp;">
                <button onClick="layer_close();" class="btn btn-default radius" type="button">&nbsp;&nbsp;取消&nbsp;&nbsp;</button>
            </div>
        </div>
    </div>
</div>



<script type="text/javascript">
    function savePassd(url) {
        var passList = new Array();
        $('input[name = oldpassword]').blur(function () {
            val = this.value;
            if (val.length == 0) {
                $(this).attr('placeholder', '原密码不能为空');
                $(this).data({ 's': '4' });
            }
            else {
                passList.push($(this).val());
                $(this).data({ 's': '1' });
            }
        });
        $('input[name = newpassword]').blur(function () {
            val = this.value;
            if (val.length == 0) {
                $(this).attr('placeholder', '新密码不能为空');
                $(this).data({ 's': '5' });
            }
            else {
                passList.push($(this).val());
                $(this).data({ 's': '1' });
            }
        });
        $('input[name = newpassword2]').blur(function () {
            val = this.value;
            if (val!= $('input[name = newpassword]').val()) {
                $(this).attr('placeholder', '两次密码不一致');
                $(this).data({ 's': '6' });
            }
            else {
                passList.push($(this).val());
                $(this).data({ 's': '1' });
            }
        });
        layer.confirm('确认要保存修改吗？', function () {
            passList.length = 0;
            $('.auth').blur();
            tot = 0;
            oldpass = 0;
            newpass = 0;
            newpass2 = 0;
            $('.auth').each(function () {
                if ($(this).data('s') == 1) {
                    tot += 1;
                }
                else if ($(this).data('s') == 4) {
                    oldpass = 1;
                }
                else if ($(this).data('s') == 5) {
                    newpass = 1;
                }
                else if ($(this).data('s') == 6) {
                    newpass2 = 1;
                }
            });
            if (oldpass == 1) {
                if (newpass == 1) {
                    if (newpass2 == 1) {
                        layer.msg('原密码不能为空！，新密码不能为空！', { icon: 5, time: 1000 });
                    }
                    else {
                        layer.msg('请完善密码信息再提交！');
                    }
                }
                else {
                    if (newpass2 == 1) {
                        layer.msg('两次密码不一致，原密码不能为空！请重新输入！', { icon: 5, time: 1000 });
                    }
                    else {
                        layer.msg('原密码不能为空！', { icon: 5, time: 1000 });
                    }
                }
            }
            else {
                if (newpass == 1) {
                    if (newpass2 == 1) {
                        layer.msg('两次密码不一致，请重新输入！', { icon: 5, time: 1000 });
                    }
                    else {
                        layer.msg('新密码不能为空！', { icon: 5, time: 1000 });
                    }
                }
                else {
                    if (newpass2 == 1) {
                        layer.msg('两次密码不一致，请重新输入！', { icon: 5, time: 1000 });
                    }
                    else if(tot == 3){
                        $.ajax({
                            type: 'POST',
                            url: url,
                            data: { "passditem": passList.toString() },
                            success: function (response) {
                                if (response == "ok") {
                                    layer.msg('密码已修改！请使用新密码登录', { icon: 1, time: 3000 });
                                    self.setInterval(function () {  // 这个方法是说在延迟两秒后执行大括号里的方法
                                        layer_close();      //关闭当前界面
                                    }, 2000); //这里2000代表两秒
                                }
                                else if (response == "nochange") {
                                    layer.msg('密码没有改变哦！还是原来的密码', { icon: 1, time: 3000 });
                                    self.setInterval(function () {  // 这个方法是说在延迟两秒后执行大括号里的方法
                                        layer_close();      //关闭当前界面
                                    }, 2000); //这里2000代表两秒
                                }
                                else if (response == "olderror") {
                                    layer.msg('原密码不正确！', { icon: 5, time: 3000 });
                                    passList.length = 0;
                                }
                                else if (response == "disagree") {
                                    layer.msg('两次密码不一致哦！重新试试看', { icon: 5, time: 3000 });
                                    passList.length = 0;
                                }
                                else if(response == "errordatabase"){
                                    layer.msg('密码修改失败！数据库出错，重新试试看', { icon: 5, time: 3000 });
                                    passList.length = 0;
                                }
                            },
                            error: function (response) {
                                layer.msg('网络错误！', { icon: 5, time: 3000 });
                                passList.length = 0;
                            }
                        });
                    }
                }
            }
        });
    }
</script>